package com.jee.project.controller;

import com.jee.project.model.Course;
import com.jee.project.model.CourseStudent;
import com.jee.project.model.CourseStudentId;
import com.jee.project.model.Student;
import com.jee.project.repo.CourseRepository;
import com.jee.project.repo.CourseStudentRepository;
import com.jee.project.repo.StudentRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/course-student")
@Tag(name = "Enrolling module", description = "选课管理模块")
public class CourseStudentController {
    Logger logger = LoggerFactory.getLogger(CourseStudentController.class);
    @Autowired
    private CourseStudentRepository courseStudentRepo;
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/course-student")
    @Operation(summary = "查询选课", description = "查询指定课程或带有选课学生数据的全部课程", parameters = {@Parameter(name = "courseId", in = ParameterIn.QUERY, example = "1")})
    public Iterable<CourseStudent> getCourseStudents(@Parameter(hidden = true) @RequestParam(value = "courseId",required = false) Course course, @Parameter(example = "2020") Integer year) {

        System.out.println(course);
        if (course == null && year == null) {
            return courseStudentRepo.findAll();
        } else if (course != null && year == null) {
            return courseStudentRepo.findByCourse(course);
        } else if (course == null && year != null) {
            return courseStudentRepo.findById_Year(year);
        } else {
            return courseStudentRepo.findByCourseAndId_Year(course, year);
        }
    }

    @DeleteMapping("/course-student")
    @Operation(summary = "移除某选课", description = "移除指定课程和指定学生的选课")
    public void deleteCourseById(@RequestBody CourseStudentId id) {
        courseStudentRepo.deleteById(id);
    }

    @PostMapping("/course-student")
    @Operation(summary = "添加选课", description = "根据提交的“课程id”和“学生id列表”，为指定课程添加", parameters = {@Parameter(name = "courseId", in = ParameterIn.QUERY, example = "1"), @Parameter(name = "studentIds", in = ParameterIn.QUERY, example = "2,3")})
    public void editCourse(@Parameter(hidden = true) @RequestParam("courseId") Course course, @Parameter(example = "2020") @RequestParam(value = "year", defaultValue = "2020", required = false) Integer year, @Parameter(hidden = true) @RequestParam("studentIds") List<Student> students) {
        // 根据选修学生students，形成多个选修实体对象courseStudents
        List<CourseStudent> courseStudents = students.stream().map(s -> new CourseStudent(course, s, year)).collect(Collectors.toList());
        // 使用存储库接口保存所有的选修关系
        courseStudentRepo.saveAll(courseStudents);
    }

    @GetMapping("/student:not-enrolled")
    @Operation(summary = "查询课程候选学生", description = "根据提交的“课程id”和查询未选择该课程的“学", parameters = @Parameter(name = "courseId", in = ParameterIn.QUERY, example = "1"))
    public List<Student> findCandidatesOfCourse(@Parameter(hidden = true) @RequestParam("courseId") Course course) {
        return courseStudentRepo.findStudentsByNotChosenCourse(course);
    }

    @GetMapping("/course")
    @Operation(summary = "为下拉列表准备候选课程")
    public Iterable<Course> getAllCourses() {
        return courseRepository.findAll();
    }

    @GetMapping("/getAllStudents")
    @Operation(summary = "为下拉列表准备候选课程")
    public Iterable<Student> getAllStudents() {
        return studentRepository.findAll();
    }


}